﻿// Copyright 2009 Mike Geise
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Falcon.Core.Helpers
{
	using System;
	using System.Text;

	public class RandomHelper
	{
		// private fields
		private readonly char[] _consonants = new[] { 'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'r', 's', 't', 'v' };
		private readonly char[] _doubleConsonants = new[] { 'c', 'd', 'f', 'g', 'l', 'm', 'n', 'p', 'r', 's', 't' };
		private readonly char[] _vowels = new[] { 'a', 'e', 'i', 'o', 'u' };
		private readonly char[] _numbers = new[] { '1', '2', '3', '4', '5', '6', '7', '8', '9', '0' };

		/// <summary>
		/// Initializes a new instance of the <see cref="RandomHelper"/> class.
		/// </summary>
		public RandomHelper()
		{

		}

		/// <summary>
		/// Creates a random string.
		/// </summary>
		/// <returns></returns>
		public string Create()
		{
			return Create(6, 2);
		}

		/// <summary>
		/// Creates a random string using the specified length.
		/// </summary>
		/// <param name="length">The length.</param>
		/// <returns></returns>
		public string Create(int length)
		{
			return Create(length, 0);
		}

		/// <summary>
		/// Creates a random string using the specified length and how many numbers will exist.
		/// </summary>
		/// <param name="length">The length.</param>
		/// <param name="numberLength">Length of the number.</param>
		/// <returns></returns>
		public string Create(int length, int numberLength)
		{
			var random = new Random();
			var value = new StringBuilder();
			var consonant = false;

			for (var i = 0; i <= length; i++)
			{
				if (value.Length > 0 && consonant == false && random.Next(100) < 10)
				{
					value.Append(_doubleConsonants[random.Next(_doubleConsonants.Length)], 2);
					consonant = true;
				}
				else if (consonant == false && random.Next(100) < 90)
				{
					value.Append(_consonants[random.Next(_consonants.Length)]);
					consonant = true;
				}
				else
				{
					value.Append(_vowels[random.Next(_vowels.Length)]);
					consonant = false;
				}
			}

			for (var i = 0; i <= numberLength; i++)
			{
				value.Append(_numbers[random.Next(_numbers.Length)].ToString());
			}

			value.Length = length + numberLength;

			return value.ToString();
		}
	}
}